<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\27 0027
 * Time: 0:05
 */
namespace app\index\controller;

use think\facade\Session;

class User extends Init
{
    public function initialize(){
        parent::initialize();
        $this->userModel = model('common/user');
        $this->extraUserInfo = model('common/extraUserInfo');
    }

    /**
     * 修改签名
     * @return \think\response\Json
     */
    public function editSign(){
        $sign = $this->request->post('sign','');
        $user_id = Session::get('user_id');
        if($this->userModel->_edit(['sign'=>$sign],['id'=>$user_id])){
            return json(['code'=>0,'msg'=>'修改成功','data'=>'']);
        }else{
            return json(['code'=>-1,'msg'=>'修改失败','data'=>'']);
        }
    }

    /**
     * 获取用户详细信息
     * @return string
     */
    public function getUserInfo(){
        $id = $this->request->get('id',0);
        $type = $this->request->get('type',0);

        $data = $this->userModel->getUserExtraInfo($id);
        $user_info['user_id'] = $id;
        $user_info['username'] = isset($data['username']) ? $data['username'] : '' ;
        $user_info['avatar'] = isset($data['avatar']) ? $data['avatar'] : '' ;
        $user_info['sex'] = isset($data['sex']) ? $data['sex'] : 0 ;
        $user_info['birthday'] = isset($data['birthday']) ? date('Y-m-d',$data['birthday']) : date('Y-m-d') ;
        $user_info['blood_type'] = isset($data['blood_type']) ? $data['blood_type'] : '' ;
        $user_info['jobs'] = isset($data['jobs']) ? $data['jobs'] : 0 ;
        $user_info['qq'] = isset($data['qq']) ? $data['qq'] : '' ;
        $user_info['wechat'] = isset($data['wechat']) ? $data['wechat'] : '' ;
        $user_info['email'] = isset($data['email']) ? $data['email'] : '' ;
        $user_info['phone'] = isset($data['phone']) ? $data['phone'] : '' ;
        $user_info['sign'] = isset($data['sign']) ? $data['sign'] : '' ;
        return json_encode(['code'=>0,'msg'=>'','data'=>$user_info]);
    }

    public function saveUserInfo(){
        $user_id = session('user_id');
        $param = $this->request->param();
        if($user_id != $param['user_id']){
            return json_encode(['code'=>-1,'msg'=>'不能修改他人信息','data'=>[]]);
        }
        $this->userModel->_edit(['username'=>$param['username'],'sign'=>$param['sign']],['id'=>$user_id]);
        $re = $this->extraUserInfo->findByUserId($user_id);
        $extraUserInfo = [
            'sex'=>isset($param['sex']) ? $param['sex'] : 0,
            'birthday'=>strtotime($param['birthday']),
            'blood_type'=>$param['blood_type'],
            'jobs'=>$param['jobs'],
            'qq'=>$param['qq'],
            'wechat'=>$param['wechat'],
            'phone'=>$param['phone'],
        ];
        if($re){
            $this->extraUserInfo->_edit($extraUserInfo,['id'=>$user_id]);
        }else{
            $extraUserInfo['user_id'] = $user_id;
            $this->extraUserInfo->_save($extraUserInfo);
        }
        return json_encode(['code'=>0,'msg'=>'保存成功!','data'=>[]]);
    }
}
